﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;

namespace 企业人事管理系统
{
    public partial class ImageChangeForm : Form
    {
        static public string initPath = Application.StartupPath + "\\BackGroud";

        private string nowImage;
        private const string addItem = "添加新的背景图片";
        private const string manageItem = "管理图片文件";
        private const string renewItem = "刷新图片预览";
        private mainForm father;
        //  private bool formSelect;

        //初始化背景图片文件夹，复制默认背景进入文件夹
        static public void initResDir()
        {
            if(!Directory.Exists(initPath))
                Directory.CreateDirectory(initPath);
            int count = Convert.ToInt32(thisResource.backNumber);
            for (int i=0;i<=count;i++)
            {
                string name = "Back" + i;
                Bitmap bitMap = (Bitmap)thisResource.ResourceManager.GetObject(name);
                string path = initPath + "\\" + name + ".jpg";
                if(!File.Exists(path))
                    bitMap.Save(path);
            }
        }

        public ImageChangeForm(mainForm f)
        {
            father = f;
            InitializeComponent();
            nowImage = initPath+"\\Back0.jpg";
            pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBoxLeft.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBoxRight.SizeMode = PictureBoxSizeMode.StretchImage;
        }

        private void ImageChangeForm_Load(object sender, EventArgs e)
        {
            load_picture();
          
        }

        private void load_picture()
        {
            string[] files = Directory.GetFiles(initPath);
            int now,last, next;
            now = 0;
            last = -1;
            next = 1;
            for(now =0;now<files.Length;now++)
            {
                if(files[now].Equals(nowImage))
                {
                    break;
                }
                //now++;
                //MessageBox.Show(files[now]);

            }
            last = now - 1;
            next = now + 1;
            last = (last+files.Length) % files.Length;
            next = next % files.Length;
            //MessageBox.Show(nowImage);


            if(pictureBox.Image!= null) pictureBox.Image.Dispose();
            if (pictureBoxLeft.Image != null)  pictureBoxLeft.Image.Dispose();
            if (pictureBoxRight.Image != null) pictureBoxRight.Image.Dispose();
            pictureBox.Image = Image.FromFile(files[now]);
            pictureBoxLeft.Image = Image.FromFile(files[last]);
            pictureBoxRight.Image = Image.FromFile(files[next]);


            for (int i = 0; i < files.Length; i++)
            {
                files[i] = Path.GetFileName(files[i]);
            }
            comboBox1.Items.Clear();
            comboBox1.Items.AddRange(files);
            comboBox1.SelectedText = files[now];
            comboBox1.Items.Add(addItem);
            comboBox1.Items.Add(manageItem);
            comboBox1.Items.Add(renewItem);
        }

        private void pictureBox_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectText = comboBox1.Items[comboBox1.SelectedIndex].ToString();
            if (selectText.Equals(addItem))
            {
                try
                {
                    OpenFileDialog open = new OpenFileDialog();
                    open.ShowDialog();
                    string sourceFile = open.FileName;
                    nowImage = initPath + "\\" + Path.GetFileName(sourceFile);
                    File.Copy(sourceFile, nowImage);
                }
                catch (Exception ex)
                {
                    return;
                }
            }
            else if(selectText.Equals(manageItem))
            {
                MessageBox.Show("由于包含其他引用，不建议修改默认图片名称或位置");
                System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("Explorer.exe");
                psi.Arguments = "/e,/select," + initPath+"\\"+Path.GetFileName(nowImage);
                System.Diagnostics.Process.Start(psi);
            }else if(selectText.Equals(renewItem))
            {

            }
            else
            {
                nowImage = initPath + "\\" + selectText;
            }
            try
            {
                load_picture();
            }
            catch
            {
                MessageBox.Show("加载图片失败,请尝试刷新后再重新选择");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Image last = null;
            try
            {
                last = father.BackgroundImage;
                father.BackgroundImage = Image.FromFile(nowImage);
                father.reLoadBack(e);
            }
            catch(Exception ex)
            {
                MessageBox.Show("背景更换失败:" + ex.Message);
            }
            if (last != null)
                last.Dispose();

            Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings["defaultBack"].Value = Path.GetFileName(nowImage);
            config.Save();
          //  MessageBox.Show(Path.GetFileName(nowImage) + " # " + config.AppSettings.Settings["defaultBack"].Value.ToString());

            Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

      
           
      
    }
}
